/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-8-10
 */
package com.flyee.controller{
	
	import com.flyee.controller.timer.AreaMagicTimer;
	import com.flyee.controller.timer.BuffMagicTimer;
	import com.flyee.controller.timer.CartoonTimer;
	import com.flyee.controller.timer.FightTimer;
	import com.flyee.controller.timer.ShootSkillTimer;
	import com.flyee.model.Charactor;
	import com.flyee.model.Monster;
	import com.flyee.service.pool.EffectDepot;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.Obsevable;
	import com.flyee.viewer.CharactorViewer;
	import com.flyee.viewer.SkillViewer;
	import com.flyee.viewer.pack.helper.ItemDataModel;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Point;	
	
	/**
	 * 人物技能控制器, 控制技能TIMER 
	 */
	public class CharacterSkillController extends Obsevable{
		
		public function CharacterSkillController(){
			
		}
		
		/**
		 * 近战技能 直接播放战斗动作 
		 */
		public function fightSkill(playerV : CharactorViewer, target : Charactor, targetV : CharactorViewer, damage : int) : void {
			var ctTimer : CartoonTimer = ObjectCollection.cartoonTimerMap.get(playerV.vid);
			var skillV : SkillViewer = new SkillViewer();			
			var timer : FightTimer = new FightTimer(50);
			timer.player = ctTimer.char;
			timer.playerV = playerV;
			timer.target = target;
			timer.targetV = targetV;
			timer.skillV = skillV;
			if(ctTimer!=null) 
				timer.label = "w" + ctTimer.getAction() + ctTimer.getDirect();
			playerV.specialContainer.addChild(skillV);
			timer.damage = damage;
			timer.callbackFn = dealDamageHandler;
			var obj : Object = {x:100, y:-50, view:skillV, timer:timer, cartoon:EffectDepot.getWeaponEffect()};
			loadMagicToContainerHandler(obj);
		}
		
		/**
		 * 表现发射类攻击技能，分3阶段，施法吟唱、魔法飞行动画、魔法命中目标动画 
		 */
		public function showShootSkill(playerV : CharactorViewer, targetV : CharactorViewer, target : Charactor, damage : int) : void {
			var skillV : SkillViewer = new SkillViewer();
			var skillTimer : ShootSkillTimer = new ShootSkillTimer(100);
			skillV.visible = false;
			ObjectCollection.currentSceneViewer.driftLayer.addChild(skillV);
			skillV.x = playerV.x;
			skillV.y = playerV.y;
			skillTimer.playerViewer = playerV;
			skillTimer.targetViewer = targetV;
			skillTimer.skillViewer = skillV;
			skillTimer.target = target;
			skillTimer.damage = damage;
			skillTimer.callbackFn = dealDamageHandler;
			skillTimer.start();
			var obj : Object = {x:-25, y:-80, view:skillV, cartoon:EffectDepot.getFireBallEffect()};
			loadMagicToContainerHandler(obj);
		}
		
		/**
		 * 表现施放范围攻击魔法 ， 2阶段： 施法吟唱、范围效果动画
		 */
		public function showAreaMagic(playerV : CharactorViewer, target : Point, targets : Array, damage : int) : void {
			var skillV : SkillViewer = new SkillViewer();
			var timer : AreaMagicTimer = new AreaMagicTimer(100);
			skillV.visible = false;
			ObjectCollection.currentSceneViewer.driftLayer.addChild(skillV);
			timer.playerViewer = playerV;
			timer.skillViewer = skillV;
			timer.targetPoint = target;
			timer.enemys = targets;
			timer.damage = damage;
			timer.callbackFn = dealDamageHandler;
			timer.start();
			var obj1 : Object = {x:23, y:-70, view:playerV, timer:timer, cartoon:EffectDepot.getFirstEffect()};
			loadMagicToAvatar(obj1);
			var obj : Object = {x:-75, y:-130, view:skillV, cartoon:EffectDepot.getStormEffect()};
			loadMagicToContainerHandler(obj);
		}
		
		/**
		 * 表现状态魔法
		 */
		public function showBuffMagic(playerV : CharactorViewer, targetV : CharactorViewer) : void {
			var target : Charactor = ObjectCollection.getCharacterByVid(targetV.vid);
			if(target == null) return;
			var skillVId:String = null;
			if(target.isBuff("4")){
				skillVId = target.getBuffByType("4");
				var leng:int = targetV.specialContainer.numChildren;
				var viewer:DisplayObject = null;
				for(var i :int = 0;i < leng;i++){
					viewer = targetV.specialContainer.getChildAt(i);
					if(viewer is SkillViewer){
						if(SkillViewer(viewer).vid == skillVId){
							targetV.specialContainer.removeChild(viewer);
							SkillViewer(viewer).dispose();
							viewer = null;
							break;	
						}
					}
				}
				target.removeBuff("4");
			}
			var skillV : SkillViewer = new SkillViewer();
			var timer : BuffMagicTimer = new BuffMagicTimer(100);
			skillV.visible = false;
			skillV.scaleX = 2;
			skillV.scaleY = 2;			
			target.addBuff("4",skillV.vid);	
			targetV.specialContainer.addChild(skillV);
			timer.playerViewer = playerV;
			timer.targetViewer = targetV;
			timer.skillViewer = skillV;
			var obj : Object = {x:-37, y:-115, view:skillV, timer:timer, cartoon:EffectDepot.getBuffEffect()};
			loadMagicToContainerHandler(obj);
		}
		
		/**技能特效加载处理*/
		public function loadMagicToContainerHandler(params : Object) : void {
			var container : SkillViewer = params.view as SkillViewer;
			container.cartoon = params.cartoon as MovieClip;
			container.addChild(container.cartoon);
			container.cartoon.x += params.x;
			container.cartoon.y += params.y;
			if(params.timer)
				params.timer.start();
		}
		
		/**技能特效加载处理*/
		public function loadMagicToAvatar(params : Object) : void {
			var container : CharactorViewer = params.view as CharactorViewer;
			var timer : AreaMagicTimer = params.timer as AreaMagicTimer;
			var cartoon : MovieClip = params.cartoon as MovieClip;
			container.specialContainer.addChild(cartoon);
			cartoon.x += params.x;
			cartoon.y += params.y;
			timer.startCartoon = cartoon;
		}
		
		/**
		 * 使用回复道具后处理回复
		 * */
		public function dealResumeHandler(player:Charactor,resume:int):void{
			var viewer:CharactorViewer = null;
			var hp:int = 0;
			if(player.gcode == ObjectCollection.master.gcode){//自己喝药
				if(ObjectCollection.master.hp == 0) return;
				viewer = ObjectCollection.getCharacterViewerByMid(ObjectCollection.master.mid);
				hp = resume + ObjectCollection.master.hp;
				if(hp > 500) hp = 500;
				viewer.playResumeEffect(resume);
				viewer.resetHP(hp);
				this.setChanged();
				this.notifyObsevers("updateHP",hp);
				ObjectCollection.master.hp = hp;
				var itemData:ItemDataModel = ObjectCollection.skillPanelViewer.drugItems.getItemAt(0) as ItemDataModel;
				var count:int = itemData.currentCapability;
				itemData.capability = count - 1;
				itemData.action = "remove";
				ObjectCollection.skillPanelViewer.drugItems.refresh();
			}else{//其他玩家喝药
				viewer = ObjectCollection.getCharacterViewerByMid(player.mid);
				if(viewer == null) return;
				hp = resume + player.hp;
				if(hp > 500) hp = 500;
				viewer.playResumeEffect(resume);
				viewer.resetHP(hp);
				player.hp = hp;
			}
			if(ObjectCollection.master.target){
				ObjectCollection.targetWindowViewer.hp = ObjectCollection.master.target.hp;
			}
		}

		/**
		 * 技能动画播放完后处理 制造伤害 
		 */
		public function dealDamageHandler(screr : Charactor, player : Charactor, damage : int) : void {
			if(player == null) return;
			if(player.hp==0)
				return ;
			if(player.hp>damage)
				player.hp -= damage;
			else{0
				player.hp = 0;
				player.skillStatus = false;
				player.moveStatus = false;
				player.waitMoveStatus = false;
				this.setChanged();
				this.notifyObsevers("playerDie", ObjectCollection.getCharacterViewerByMid(player.mid));
			}
			if(player == ObjectCollection.master){
				this.setChanged();
				this.notifyObsevers("updateHP",player.hp);
			}else if(player == ObjectCollection.master.target){
				ObjectCollection.targetWindowViewer.hp = player.hp;
			}else{
				
			}
		
			var master:CharactorViewer = ObjectCollection.getCharacterViewerByMid(player.mid);
			if(master == null) return;
			master.playHurtEffect(damage);
			master.resetHP(player.hp);
			//待定
			if(player is Monster){
				this.setChanged();
				this.notifyObsevers("joinFight", screr, player);
				var monster:CharactorViewer = ObjectCollection.getCharacterViewerByMid(player.mid);
				monster.playHurtEffect(damage);
				monster.resetHP(player.hp);
			}
		}
		
	}
}